package com.itany.controller;

import com.github.pagehelper.PageInfo;
import com.itany.pojo.ManagerUser;
import com.itany.pojo.Role;
import com.itany.service.ManagerUserService;
import com.itany.service.RoleService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Author：汤小洋
 * Date：2019-09-18 上午9:58
 * Description：<描述>
 */

@Controller
@RequestMapping("/managerUser")
public class ManagerUserController {


    @Autowired
    private ManagerUserService managerUserService;
    @Autowired
    private RoleService roleService;




    //Shiro 登录
    @RequestMapping("/goLogin")
    public String login(ManagerUser managerUser, Model model)throws  Exception{

        Subject subject = SecurityUtils.getSubject();
        UsernamePasswordToken token = new UsernamePasswordToken(managerUser.getUsername(),managerUser.getPassword());
        subject.login(token);
            ManagerUser m= managerUserService.findByUserNameAndPassword(managerUser);
            System.out.println(m+"111111111111111111111");
        if(subject.isAuthenticated()){

            model.addAttribute("user",managerUser.getUsername());
            model.addAttribute("id",m.getId());
        }



        return "index";

    }

    // @RequestMapping("/goLogin")
    // public String login(ManagerUser managerUser, Model model){
    //
    //   if(null==managerUser.getUsername()){
    //       model.addAttribute("error","用户名不能为空");
    //      return "login";
    //   }
    //  if(null==managerUser.getPassword()){
    //       model.addAttribute("error","密码不能为空");
    //       return "login";
    //   }
    //
    //   ManagerUser m= managerUserService.findByUserNameAndPassword(managerUser);
    //   if(null==m){
    //   model.addAttribute("error","用户名或密码错误");
    //    return "login";
    //
    //    }
    //     model.addAttribute("user",m);
    //     return "index";
    //
    // }

    @RequestMapping("findAll")
    @ResponseBody
    public Map<String,Object> findAll(@RequestParam(defaultValue="1")Integer page,
                                      @RequestParam(defaultValue="10")Integer rows,ManagerUser managerUser){
        Map<String,Object> map = new HashMap<String, Object>();
        PageInfo<ManagerUser> info=managerUserService.findAll(page, rows,managerUser);
        map.put("total", info.getTotal());
        map.put("rows", info.getList());
        return map;
    }


    @RequestMapping("findAllRole")
    @ResponseBody
    public List<Role> findAllRole(){

        List<Role> roles=roleService.findAllRole();


        return roles;
    }


    @RequestMapping("addUser")
    public String addUser(ManagerUser managerUser,@RequestParam("myPermissionId") String ids){


        managerUserService.addUser(managerUser,ids);

        managerUserService.addRoleById(managerUser.getId(),ids);

        return "manager";
    }

    @RequestMapping("modifyUser")
    public String modifyUser(ManagerUser managerUser,@RequestParam("myPermissionId") String ids){



        managerUserService.modifyUser(managerUser,ids);


        return "manager";
    }

} 